package com.codefish.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codefish.entity.OutMoneyType;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.OutMoneyTypeService;
import com.codefish.util.Assert;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 支出类型
 *
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-07-09 11:32:08
 */
@RestController
@RequestMapping("/outMoneyType")
public class OutMoneyTypeController {

    @Autowired
    private OutMoneyTypeService outMoneyTypeService;

    /**
     * 新增单个
     *
     * @param token
     * @param outMoneyType
     * @return
     */
    @PostMapping("/saveOne")
    public R saveOne(@RequestHeader("Authentication") String token
            , @RequestBody OutMoneyType outMoneyType) {

        Assert.isNull(outMoneyType.getTypeName(), "typeName不能为空");
        Assert.isNull(outMoneyType.getMoneyAccountId(), "moneyAccountId不能为空");
        Assert.isNull(outMoneyType.getOutMoneyClass(), "outMoneyClass不能为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");
        Assert.isNull(user.getFamilyId(), "当前用户familyId无效");

        // set createTime / familyId
        outMoneyType.setCreateId(user.getId());
        outMoneyType.setFamilyId(user.getFamilyId());

        Integer id = outMoneyTypeService.saveOne(outMoneyType);

        if (null == id || id <= 0) {
            return R.error("新增失败");
        }

        return R.ok().put("id", id);
    }

    /**
     * 修改单个
     *
     * @param token
     * @param outMoneyType
     * @return
     */
    @PostMapping("/updateOne")
    public R updateOne(@RequestHeader("Authentication") String token
            , @RequestBody OutMoneyType outMoneyType) {
        Assert.isNull(outMoneyType.getId(), "id不能为空");
        Assert.isNull(outMoneyType.getTypeName(), "typeName不能为空");
        Assert.isNull(outMoneyType.getMoneyAccountId(), "moneyAccountId不能为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");

        // set createTime / familyId
        outMoneyType.setUpdateId(user.getId());
        outMoneyType.setUpdateTime(LocalDateTime.now());

        boolean flag = outMoneyTypeService.updateOne(outMoneyType);

        if (!flag) {
            return R.error("修改失败");
        }

        return R.ok("修改成功");
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam("id") Integer id) {

        boolean flag = outMoneyTypeService.deleteOne(id);

        if (!flag) {
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }

    /**
     * 分页条件查询
     *
     * @param token
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPageByParams")
    public R getPageByParams(@RequestHeader("Authentication") String token
            , @RequestParam("pageNo") Integer pageNo
            , @RequestParam("pageSize") Integer pageSize) {

        Assert.isNull(pageNo,"pageNo不能为空");
        Assert.isNull(pageSize,"pageSize不能为空");

        SysUserTokenEntity user = JWTService.getUser(token);

        Assert.isNull(user.getFamilyId(),"当前用户家庭无效");

        IPage<OutMoneyType> retPage = outMoneyTypeService.getPageByParams(user.getFamilyId(),pageNo,pageSize);

        return R.ok().put("data",retPage);
    }
}
